//
//  TTXMLNode.h
//  Tint
//
//  Created by Kevin Wu on 4/19/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <libxml/tree.h>
#import "TTCore.h"
#import "TTAdditions.h"


typedef enum {
    TTXMLNodeInvalid = 0, 
    TTXMLNodeDocument, 
    TTXMLNodeElement, 
    TTXMLNodeAttribute, 
    TTXMLNodeText, 
    TTXMLNodeNamespace, 
    TTXMLNodeProcessingInstruction, 
    TTXMLNodeComment, 
    TTXMLNodeDTD, 
    TTXMLNodeEntityDeclaration, 
    TTXMLNodeAttributeDeclaration, 
    TTXMLNodeElementDeclaration, 
    TTXMLNodeNotationDeclaration
} TTXMLNodeType;


@interface TTXMLNode : NSObject {
    xmlNodePtr _XMLNode;
    
    NSString *_cachedName;
    NSArray *_cachedChildren;
    NSArray *_cachedAttributes;
}

@property(nonatomic, assign) xmlNodePtr XMLNode;
@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy) NSString *value;
@property(nonatomic, readonly) TTXMLNodeType type;
@property(nonatomic, readonly) NSString *XMLString;

- (id)initWithXMLNode:(xmlNodePtr)XMLNode;

+ (TTXMLNode *)nodeWithXMLNode:(xmlNodePtr)XMLNode;


- (NSUInteger)childCount;
- (NSArray *)children;
- (TTXMLNode *)childAtIndex:(NSUInteger)index;


- (TTXMLNode *)parent;

- (TTXMLNode *)firstChild;
- (TTXMLNode *)firstChildNamed:(NSString *)name;
- (TTXMLNode *)lastChild;
- (TTXMLNode *)lastChildNamed:(NSString *)name;

- (TTXMLNode *)firstAttribute;
- (TTXMLNode *)lastAttribute;

- (TTXMLNode *)previousSibling;
- (TTXMLNode *)previousSameNameSibling;
- (TTXMLNode *)nextSibling;
- (TTXMLNode *)nextSameNameSibling;

- (NSArray *)elements;
- (NSArray *)elementsForName:(NSString *)name;
- (NSArray *)attributes;
- (TTXMLNode *)attributeForName:(NSString *)name;


- (TTXMLNode *)addElementWithName:(NSString *)name value:(NSString *)value;
- (TTXMLNode *)addAttributeWithName:(NSString *)name value:(NSString *)value;
- (TTXMLNode *)addTextWithValue:(NSString *)value;
- (TTXMLNode *)addChild:(TTXMLNode *)child;
- (void)removeChild:(TTXMLNode *)child;

@end
